package com.zb.jnlxc.service;

import java.io.File;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.aliyun.oss.model.OSSObject;
import com.zb.util.OSSServiceClient;
import net.sf.jasperreports.engine.JRException;

import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.ZLHW.base.service.BaseService;
import com.zb.jnlxc.JasperReport.CreateReportPDF;
import com.zb.jnlxc.dao.PaichanOrderDetailDAO;
import com.zb.jnlxc.model.OrderDetail;
import com.zb.jnlxc.model.OrderForm;
import com.zb.jnlxc.model.PaiChanRecord;
import com.zb.jnlxc.model.PaichanOrderDetail;

@Service
public class ReportService extends BaseService {
    @Value("${JNLXC.imagesRoot}")
    private String                imagesRoot;

    @Value("${JNLXC.bucketName}")
    private String bucketName ;

    @Resource
    private PaichanOrderDetailDAO paichanOrderDetailDAO;

    @Resource
    private OSSServiceClient ossServiceClient;

    /**
     * 以流的方式输出
     * @param paiChanRecord
     * @param os
     * @throws JRException
     * @throws DocumentException
     */
    public void exportProductRecord(PaiChanRecord paiChanRecord, OutputStream os)
                                                                                 throws JRException,
                                                                                 DocumentException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        OrderForm orderForm = paiChanRecord.getOrderForm();
        List<PaichanOrderDetail> paichanOrderDetails = paichanOrderDetailDAO
            .findByPaichan(paiChanRecord.getDbId());
        List<PaichanOrderDetail> list = new ArrayList();
        for (PaichanOrderDetail orderDetail : paichanOrderDetails) {
            list.add(orderDetail);
        }
        while (list.size() < 11) {
            list.add(new PaichanOrderDetail());
        }
        Map parameters = new HashMap();
        parameters.put("ReportTitle", "生产跟踪单");
        parameters.put("time", sdfTime.format(paiChanRecord.getCreateDate()));
        parameters.put("schemeCode", paiChanRecord.getOrderForm().getScheme().getName());
        parameters.put("client", paiChanRecord.getOrderForm().getClient().getClientCode());
        parameters.put("wcomment", paiChanRecord.getOrderForm().getMcomment());
        if (paiChanRecord.getCharger() != null)
            parameters.put("Charger", paiChanRecord.getCharger().getName());
        else
            parameters.put("Charger", "");
        parameters.put("orderForm", orderForm);
        parameters.put("recordid", paiChanRecord.getCode());
        OSSObject ossObject = ossServiceClient.getObject(bucketName,imagesRoot + orderForm.getScheme().getBmpSrc());
        if(ossObject!=null){
            parameters.put("imageContent",ossObject.getObjectContent());
        }
        parameters.put("pack", orderForm.getPack().getName());
        parameters.put("surfaceProcess", orderForm.getSurfaceProcess().getName());
        parameters.put("material", orderForm.getMaterial().getName());
        if (orderForm.getScheme().getKpm() != null)
            parameters.put("kpm", orderForm.getScheme().getKpm().toString());
        if (orderForm.getPreSendDate() != null)
            parameters.put("preSendDate", sdf.format(orderForm.getPreSendDate()));
        new CreateReportPDF("WebappReport", "a.pdf", parameters, list).exportReportToPdfStream(os);
    }
}
